<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>享元模式典型应用之分页（前端）----未使用享元模式的</title>
</head>
<body>
    <div id="news"></div>
    <button id="nextBtn" onclick="goNext()">下一页</button>
    <script>
        let list = [];
        for(var i = 0; i< 20; i++) {
            list.push(`新闻----第${i}条`)
        };
        let pageNum = 0;
        let pageSize = 5;
        // 遍历数据将所有数据节点插入并且不显示
        for(var i = 0; i < list.length; i++) {
            let li = document.createElement('li');
            li.style.display = 'none';
            li.innerHTML = list[i];
            document.getElementById('news').appendChild(li);
        };
        // 下一页
        function goNext() {
            if (pageNum > Math.floor(list.length / pageSize) - 1) return alert('已经是最后一页了')
            pageNum++;
            let pageStartNum = (pageNum - 1) * pageSize;
            let lis = document.getElementsByTagName('li');
            for (var i = 0; i < lis.length; i++) {
                if (i >= pageStartNum && i < pageStartNum + pageSize) {
                    lis[i].style.display = 'block';
                }
                else {
                    lis[i].style.display = 'none';
                }
            }
        }
    </script>
</body>
</html>